using Coding4Fun.Phone.Controls;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.UserContrl;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Navigation;
namespace MicroMsg.UI.Util
{
	public class BasePage : PhoneApplicationPage
	{
		private const string TAG = "BasePage";
		private static Dictionary<string, PageNavSpeed> mapPage = new Dictionary<string, PageNavSpeed>();
		public UIActionService actionService;
		private bool isBackFromTombstone = true;
		protected bool mBIsGoingToMainPageFromTombstone;
		public MMThemeTypes mPageTheme = MMTheme.getThemeType();
		private bool mBIsSetSysTrayColor;
		private IApplicationBar mBar;
		public static readonly DependencyProperty RootFrameTransformProperty = DependencyProperty.Register("RootFrameTransform", typeof(double), typeof(BasePage), new PropertyMetadata(new PropertyChangedCallback(BasePage.OnRootFrameTransformChanged)));
		private EventWatcher msgWatcher;
		private static bool sBCanDoBackSysTask = false;
		private static List<string> sListStrToShow = new List<string>();
		private void pageLoadStart()
		{
			string name = base.GetType().get_Name();
			if (!BasePage.mapPage.ContainsKey(name))
			{
				BasePage.mapPage.set_Item(name, new PageNavSpeed());
			}
			BasePage.mapPage.get_Item(name).start = DebugEx.getTimeSpan(0L);
		}
		private void pageLoadEnd()
		{
			string name = base.GetType().get_Name();
			if (!BasePage.mapPage.ContainsKey(name))
			{
				return;
			}
			BasePage.mapPage.get_Item(name).loadTime = DebugEx.getTimeSpan(BasePage.mapPage.get_Item(name).start);
		}
		public static void logPageSpeed()
		{
			using (Dictionary<string, PageNavSpeed>.Enumerator enumerator = BasePage.mapPage.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, PageNavSpeed> arg_15_0 = enumerator.get_Current();
				}
			}
		}
		public static string getPageNavInfo()
		{
			string text = "";
			using (Dictionary<string, PageNavSpeed>.Enumerator enumerator = BasePage.mapPage.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, PageNavSpeed> current = enumerator.get_Current();
					text = string.Concat(new object[]
					{
						text,
						current.get_Key(),
						"=",
						current.get_Value().loadTime,
						"\n"
					});
				}
			}
			return text;
		}
		public BasePage()
		{
			this.pageLoadStart();
			this.actionService = new UIActionService(this);
			base.add_Loaded(new RoutedEventHandler(this.BasePage_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.BasePage_Unloaded));
			base.add_LayoutUpdated(new EventHandler(this.BasePage_OnLayoutUpdated));
			this.OnSetTransition();
			ShowObjectInMemory.AddOneObjectRecord(this);
			if (!(this is MainPage))
			{
				MMTheme.GetInstance().DelayInitThemes();
			}
		}
		~BasePage()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private void BasePage_Unloaded(object sender, RoutedEventArgs e)
		{
			this.MsgAlertUnit();
			if (!this.isBackFromTombstone)
			{
				this.OnPageUnLoaded(sender, e);
			}
		}
		private void BasePage_Loaded(object sender, RoutedEventArgs e)
		{
			this.setAppBarStyle(base.get_ApplicationBar());
			this.MsgAlertInit();
			this.pageLoadEnd();
			BasePage.ShowToastsTaskInThisPage();
			if (!this.mBIsGoingToMainPageFromTombstone)
			{
				this.OnPageLoaded(sender, e);
			}
		}
		protected virtual void OnPageLoaded(object sender, RoutedEventArgs e)
		{
		}
		protected virtual void OnPageUnLoaded(object sender, RoutedEventArgs e)
		{
		}
		private void BasePage_OnLayoutUpdated(object sender, EventArgs e)
		{
		}
		protected virtual void OnSetTransition()
		{
		}
		protected virtual void OnClearTransition()
		{
		}
		private void SetTransition(UIElement element)
		{
			BasePage.SetInTransition(element);
			BasePage.SetOutTransition(element);
		}
		private static void SetInTransition(UIElement element)
		{
			NavigationInTransition navigationInTransition = new NavigationInTransition();
			TurnstileTransition turnstileTransition = new TurnstileTransition();
			turnstileTransition.Mode = TurnstileTransitionMode.BackwardIn;
			TurnstileTransition turnstileTransition2 = new TurnstileTransition();
			turnstileTransition2.Mode = TurnstileTransitionMode.ForwardIn;
			navigationInTransition.Backward = turnstileTransition;
			TransitionService.SetNavigationInTransition(element, navigationInTransition);
		}
		private static void SetOutTransition(UIElement element)
		{
			NavigationOutTransition navigationOutTransition = new NavigationOutTransition();
			TurnstileTransition turnstileTransition = new TurnstileTransition();
			turnstileTransition.Mode = TurnstileTransitionMode.BackwardOut;
			TurnstileTransition turnstileTransition2 = new TurnstileTransition();
			turnstileTransition2.Mode = TurnstileTransitionMode.ForwardOut;
			navigationOutTransition.Backward = turnstileTransition;
			TransitionService.SetNavigationOutTransition(element, navigationOutTransition);
		}
		public void GoToPage(string string_0, object[] object_0, bool isRemoveMe = false)
		{
			if (App.sBAppIsInForground)
			{
				string text = PageManager.PutArg(new BasePageArg
				{
					IsRemoveLastPage = isRemoveMe,
					UserData = object_0
				});
				string text2 = string_0 + string.Format("?{0}={1}", "ARGID", text);
				try
				{
					base.get_NavigationService().Navigate(new Uri(text2, 2));
				}
				catch (Exception ex)
				{
					Log.smethod_5(base.GetType().get_Name(), "NavigationService exception catched:" + ex.get_Message());
				}
			}
		}
		protected override void OnNavigatedTo(NavigationEventArgs navigationEventArgs_0)
		{
			PageManager.getInstance();
			base.OnNavigatedTo(navigationEventArgs_0);
			if (navigationEventArgs_0.get_NavigationMode() != 1)
			{
				if (navigationEventArgs_0.get_NavigationMode() == null)
				{
					PageManager.getInstance().Add(this);
					BasePageArg basePageArg = PageManager.GetArgOnce(this) as BasePageArg;
					object[] object_ = null;
					if (basePageArg != null)
					{
						object_ = basePageArg.UserData;
						if (basePageArg.IsRemoveLastPage)
						{
							PageManager.getInstance().PopPage();
						}
					}
					this.setSystemTrayStyle();
					this.OnPageIn(object_);
					PageManager.getInstance().DumpPageStack();
				}
				return;
			}
			if (!this.isBackFromTombstone)
			{
				this.OnPageBack();
				return;
			}
			Log.smethod_5("BasePage", "BackFromTombstone");
			PageManager.getInstance().PopAllPages(false);
			if (AccountMgr.hasValidAccount())
			{
				if (this is MainPage)
				{
					PageManager.getInstance().Add(this);
					this.setSystemTrayStyle();
					this.OnPageIn(new object[]
					{
						true
					});
					return;
				}
				this.mBIsGoingToMainPageFromTombstone = true;
				string text = "/Source/UI/MainPage.xaml";
				string text2 = PageManager.PutArg(new BasePageArg
				{
					IsRemoveLastPage = true,
					UserData = new object[]
					{
						true
					}
				});
				text += string.Format("?{0}={1}", "ARGID", text2);
				((App)Application.get_Current()).RootFrame.Navigate(new Uri(text, 2));
				return;
			}
			else
			{
				if (this is LoginMainView)
				{
					PageManager.getInstance().Add(this);
					this.setSystemTrayStyle();
					this.OnPageIn(new object[0]);
					return;
				}
				string text3 = "/Source/UI/LoginViews/LoginMainView.xaml";
				string text4 = PageManager.PutArg(new BasePageArg
				{
					IsRemoveLastPage = true,
					UserData = new object[0]
				});
				text3 += string.Format("?{0}={1}", "ARGID", text4);
				((App)Application.get_Current()).RootFrame.Navigate(new Uri(text3, 2));
				return;
			}
		}
		protected override void OnNavigatedFrom(NavigationEventArgs navigationEventArgs_0)
		{
			this.isBackFromTombstone = false;
			if (navigationEventArgs_0.get_NavigationMode() == 1)
			{
				PageManager.getInstance().RemovePage(this);
				base.OnNavigatedFrom(navigationEventArgs_0);
				this.OnPageOut();
				return;
			}
			if (navigationEventArgs_0.get_NavigationMode() == null)
			{
				this.OnPageForward(navigationEventArgs_0.get_Uri());
			}
		}
		protected virtual void OnPageIn(object[] object_0)
		{
		}
		protected virtual void OnPageOut()
		{
		}
		protected virtual void OnPageForward(Uri targetPage)
		{
		}
		protected virtual void OnPageBack()
		{
		}
		public void DoPageDestory()
		{
			this.OnPageOut();
		}
		private void setSystemTrayStyle()
		{
			if (this.mBIsSetSysTrayColor && this.mPageTheme == MMTheme.getThemeType())
			{
				return;
			}
			this.mBIsSetSysTrayColor = true;
			this.mPageTheme = MMTheme.getThemeType();
			this.setSystemTrayColor();
		}
		protected virtual void setSystemTrayColor()
		{
			if (MMThemeTypes.COOL_BLACK_THEME == MMTheme.getThemeType())
			{
				SystemTray.set_ForegroundColor(Color.FromArgb(255, 170, 170, 170));
				return;
			}
			if (MMTheme.getThemeType() == MMThemeTypes.NORMAL_THEME)
			{
				SystemTray.set_ForegroundColor(Color.FromArgb(255, 85, 85, 85));
			}
		}
		public static void setSystemTrayDefault()
		{
		}
		public static void setSystemTrayColorInDialog()
		{
		}
		public virtual void setAppBarStyle(IApplicationBar appbar)
		{
			if (this.mBar == appbar && this.mPageTheme == MMTheme.getThemeType())
			{
				return;
			}
			this.mPageTheme = MMTheme.getThemeType();
			if (appbar != null)
			{
				this.mBar = appbar;
				this.mBar.set_Opacity(0.99);
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
				if (this.mBar.get_Buttons().get_Count() == 0)
				{
					this.mBar.set_Mode(1);
					return;
				}
				this.mBar.set_Mode(0);
			}
		}
		public static void SetAppBarStyle(IApplicationBar appbar)
		{
			if (appbar != null)
			{
				appbar.set_Opacity(0.99);
				appbar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				appbar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
				if (appbar.get_Buttons().get_Count() == 0)
				{
					appbar.set_Mode(1);
					return;
				}
				appbar.set_Mode(0);
			}
		}
		protected void EndListenForSipChanged()
		{
			base.ClearValue(BasePage.RootFrameTransformProperty);
		}
		protected void BeginListenForSIPChanged()
		{
			PhoneApplicationFrame rootFrame = (Application.get_Current() as App).RootFrame;
			Binding binding = new Binding("Y");
			binding.set_Source((rootFrame.get_RenderTransform() as TransformGroup).get_Children().get_Item(0) as TranslateTransform);
			base.SetBinding(BasePage.RootFrameTransformProperty, binding);
		}
		private static void OnRootFrameTransformChanged(DependencyObject source, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			BasePage basePage = source as BasePage;
			if (basePage == null)
			{
				return;
			}
			double num = (double)dependencyPropertyChangedEventArgs_0.get_NewValue();
			basePage.RootFrameChange(num * -1.0);
		}
		protected virtual void RootFrameChange(double double_0)
		{
		}
		protected virtual bool IsAlertMsgNeedShow(ChatMsg chatMsg_0)
		{
			return true;
		}
		private void MsgAlertInit()
		{
			this.msgWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.on_NewMsg_EventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_ADD, this.msgWatcher);
		}
		private void MsgAlertUnit()
		{
			EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_ADD, this.msgWatcher);
		}
		private void on_NewMsg_EventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject == null)
			{
				return;
			}
			if (listObject.get_Count() <= 0)
			{
				return;
			}
			ChatMsg chatMsg = listObject.get_Item(0);
			if (chatMsg.strTalker == null || chatMsg.strTalker.Contains(ConstantValue.TAG_BOTTLE))
			{
				return;
			}
			if (chatMsg == null)
			{
				return;
			}
			if (chatMsg.nIsSender == 1)
			{
				return;
			}
			if (!chatMsg.IsSupport())
			{
				return;
			}
			if (!GConfigMgr.settings.bIsNewMsgNotifyOn)
			{
				return;
			}
			if (!this.IsAlertMsgNeedShow(chatMsg))
			{
				return;
			}
			string text;
			if (AccountMgr.DisablePushMsgDetail)
			{
				text = strings.ChatNewMsgHint;
			}
			else
			{
				if (EmojiManager.GetInstance().IsEmojiMsg(chatMsg))
				{
					text = string.Format(strings.ChatNewEmjHint, ContactHelper.getDisplayName(chatMsg.strTalker, null));
				}
				else
				{
					if (chatMsg.strTalker.Equals("newsapp") && chatMsg.nIsSender != 1)
					{
						TXNews tXNews = NewsMgr.parseNewsXml(chatMsg.strMsg);
						text = null;
						if (tXNews != null)
						{
							text = tXNews.strTitle;
						}
					}
					else
					{
						if (ContactHelper.isQQMailContact(chatMsg.strTalker))
						{
							QQMail qQMail = QQMailMgr.parseMailXml(chatMsg.strMsg);
							text = null;
							if (qQMail != null)
							{
								text = string.Concat(new string[]
								{
									strings.Plugin_qqmail_title,
									" ",
									qQMail.strSender,
									":",
									qQMail.strTitle
								});
							}
						}
						else
						{
							if (ContactHelper.isBlogAppContact(chatMsg.strTalker))
							{
								List<TXNews> list = BlogConversationMgr.convertChatMsg2BlogMsgList(chatMsg);
								text = null;
								if (list != null && list.get_Count() > 0)
								{
									text = list.get_Item(0).strTitle;
								}
							}
							else
							{
								if (chatMsg.IsAppMsg())
								{
									text = strings.ChatNewMsgHint;
								}
								else
								{
									text = ChatMsgMgr.GetNotifyContent(chatMsg);
								}
							}
						}
					}
				}
			}
			if (text == null)
			{
				return;
			}
			string text2 = text.Replace("\n", "");
			text2 = text2.Replace("\r", "");
			ToastPrompt toastPrompt = MessageBoxManager.ShowToast(text2, 5000, null, true);
			toastPrompt.set_Tag(chatMsg.strTalker);
			toastPrompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(this.tempToast_Completed);
		}
		private void tempToast_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
		{
			if (PopUpResult.Ok == e.PopUpResult && sender != null && sender is ToastPrompt)
			{
				ToastPrompt toastPrompt = sender as ToastPrompt;
				if (toastPrompt.get_Tag() != null && toastPrompt.get_Tag() is string)
				{
					string username = toastPrompt.get_Tag() as string;
					if (ContactHelper.isNewsContact(username))
					{
						this.GoToPage("/Source/Plugin/TXNews/UI/NewsSessionView.xaml", new object[]
						{
							1
						}, false);
						return;
					}
					if (ContactHelper.isBlogAppContact(username))
					{
						this.GoToPage("/Source/Plugin/TXBlogRead/UI/TXBlogReadView.xaml", null, false);
						return;
					}
					if (ContactHelper.isQQMailContact(username))
					{
						this.GoToPage("/Source/Plugin/QQMail/UI/QQMailSessionView.xaml", new object[]
						{
							1
						}, false);
						return;
					}
					ChatContainerPage.GoToChatPage(new BasePage(), username, null, null, null);
				}
			}
		}
		public void GoBack()
		{
			if (base.get_NavigationService().get_CanGoBack())
			{
				try
				{
					base.get_NavigationService().GoBack();
				}
				catch (Exception ex)
				{
					Log.smethod_5(base.GetType().get_Name(), "NavigationService.GoBack exception catched:" + ex.get_StackTrace());
				}
			}
		}
		public static bool CanDoBackSysTask()
		{
			return BasePage.sBCanDoBackSysTask;
		}
		public static void DoBackSysTask()
		{
			BasePage.sBCanDoBackSysTask = false;
		}
		public static void EnableBackSysTask()
		{
			BasePage.sBCanDoBackSysTask = true;
		}
		public static void ShowToastInNextTopPage(string strContent)
		{
			BasePage.sListStrToShow.Add(strContent);
		}
		public static void ShowToastsTaskInThisPage()
		{
			using (List<string>.Enumerator enumerator = BasePage.sListStrToShow.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					MessageBoxManager.ShowToast(current, 2500, null, false);
				}
			}
			BasePage.sListStrToShow.Clear();
		}
	}
}
